<template>
  <page-header-wrapper :title="false">
    <a-card title="业务统计">
      <a-tabs v-model="tab" type="card" @change="onTab">
        <a-tab-pane :key="20" tab="已开启"></a-tab-pane>
        <a-tab-pane :key="10" tab="未开启"></a-tab-pane>
        <a-tab-pane :key="30" tab="已停止"></a-tab-pane>
      </a-tabs>
      <template slot="extra">
        <a-button size="small" type="primary" icon="import" @click="onImt">导入成本</a-button>
      </template>
      <div class="table-page-search-wrapper">
        <a-form-model :model="queryParam" layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="合同编号">
                <a-input v-model="queryParam['filter[contract_no]']" placeholder="输入合同编号" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="业务编号">
                <a-input v-model="queryParam['filter[business_no]']" placeholder="输入业务编号" />
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="业务开启时间">
                  <a-range-picker v-model="queryParam['open_time']" @change="onCheckAt" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="付成本时间">
                  <a-range-picker v-model="queryParam['cost_time']" @change="onCheckCost" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="审核时间">
                  <a-range-picker v-model="queryParam['check_time']" @change="onCheck" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="选择业务">
                  <a-select v-model="queryParam['filter[business_id]']" placeholder="选择要导入成本的业务">
                    <a-select-option v-for="(item,index) in businessOpts" :key="index" :value="item.business_id">{{item.name}}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
                <span class='table-page-search-submitButtons' :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                  <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                  <a-button style="margin-left: 8px" @click="resetSearch">重置</a-button>
                  <a @click="toggleAdvanced" style="margin-left: 8px">
                    {{ advanced ? '收起' : '展开' }}
                    <a-icon :type="advanced ? 'up' : 'down'" />
                  </a>
                </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="table-operator">
        <s-table
          bordered
          ref="table"
          size="middle"
          :row-key="(record) => record.cb_id"
          :columns="businessColumns"
          :data="businessData"
        >
          <div slot="title">
            <a-button icon="export" size="small" type="primary" @click="onExport">导出成本</a-button>
          </div>
          <template slot="username" slot-scope="text, record">
            <a-tag>{{text}}</a-tag><br>
            <a-tag>{{record.group_type}}组</a-tag><br>
            <a-tag>{{record.leader_name}}</a-tag>
          </template>
          <template slot="first_cost" slot-scope="text,record">
            <a-tag color="red">{{record.cost_fee[0]}}</a-tag><br>
          </template>
          <template slot="first_cost_time" slot-scope="text,record">
            <a-tag color="blue">{{record.cost_payment_time[0]}}</a-tag><br>
          </template>
          <template slot="second_cost" slot-scope="text,record">
            <a-tag color="red">{{record.cost_fee[1]}}</a-tag>
          </template>
          <template slot="second_cost_time" slot-scope="text,record">
            <a-tag color="blue">{{record.cost_payment_time[1]}}</a-tag>
          </template>
          <template slot="first_payment" slot-scope="text,record">
            {{record.payment_time[0]}}
          </template>
          <template slot="last_payment" slot-scope="text,record">
            <div v-if="record.payment_time.length > 2">{{record.payment_time[record.payment_time.length - 1]}}</div>
          </template>
        </s-table>
      </div>
    </a-card>
    <a-modal
      title="批量导入已付成本"
      v-model="visible"
      :confirm-loading="importLoading"
      @ok="submitImport"
    >
      <a-form-model
        :model="uploadForm"
      >
        <a-form-model-item label="选择要导入的CSV文件">
          <a-upload
            accept=".csv"
            name="file"
            :multiple="true"
            :headers="headers"
            :action="importUrl"
            @change="onImport"
          >
            <a-button size="small" :loading="loading" type="primary" icon="import">批量导入已付成本</a-button>
          </a-upload>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { STable } from '@/components'
// import { getStatsBusiness } from '@/api/stats'
import statsColumns from '@/columns/stats'
import apis from '@/api'
import storage from 'store'
import { getStatsBusiness, importFees } from '@/api/stats'
import { ACCESS_TOKEN } from '@/store/mutation-types'

export default {
  components: {
    STable
  },
  name: 'StatsBusiness',
  data () {
    return {
      tab: 20,
      loading: false,
      importLoading: false,
      businessColumns: statsColumns.business,
      businessData: parameters => {
        return getStatsBusiness(Object.assign(parameters, this.queryParam)).then(res => {
          this.businessOpts = res.result.businessOpts
          return res.result
        })
      },
      businessOpts: {},
      importForm: {},
      queryParam: {},
      advanced: true,
      exportUrl: apis.ExportData,
      uploadUrl: apis.Upload,
      importUrl: apis.Import,
      accessToken: storage.get(ACCESS_TOKEN),
      headers: {
        'Access-Token': storage.get(ACCESS_TOKEN),
        'Company': storage.get('company')
      },
      company: storage.get('company'),
      visible: false,
      uploadForm: {}
    }
  },
  created () {
    this.queryParam['filter[status1]'] = 20
  },
  methods: {
    onTab (key) {
      this.queryParam['filter[status1]'] = key
      this.$refs.table.refresh(true)
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    resetSearch () {
      this.queryParam = {}
      this.$refs.table.refresh(true)
    },
    onCheckAt (date, dateString) {
      this.queryParam['filter[open_time_start]'] = dateString[0]
      this.queryParam['filter[open_time_end]'] = dateString[1]
    },
    onCheckCost (date, dateString) {
      this.queryParam['filter[cost_time_start]'] = dateString[0]
      this.queryParam['filter[cost_time_end]'] = dateString[1]
    },
    onCheck (date, dateString) {
      this.queryParam['filter[check_time_start]'] = dateString[0]
      this.queryParam['filter[check_time_end]'] = dateString[1]
    },
    submitImport () {
      const app = this
      const data = {}
      data['file[value]'] = app.uploadForm.file
      app.importLoading = true
      importFees(app.queryParam['filter[business_id]'], data).then(res => {
        if (res.status === 0) {
          app.visible = false
          app.uploadForm = {}
          app.$refs.table.refresh(true)
        }
      }).finally(() => {
        app.importLoading = false
      })
    },
    onImt () {
      this.visible = true
    },
    onImport (info) {
      if (info.file.status === 'uploading') {
        this.loading = true
      }
      if (info.file.status === 'done') {
        this.loading = false
        this.uploadForm['file'] = info.file.response.result.url
      }
    },
    onExport () {
      let params = this.exportUrl + '?access_token=' + this.accessToken + '&type=2' + '&company=' + this.company
      for (const k in this.queryParam) {
        params = params + '&' + k + '=' + this.queryParam[k]
      }
      window.open(process.env.VUE_APP_API_BASE_URL + params)
    }
  }
}
</script>

<style scoped>

</style>
